import { _decorator, Component, Node, Label, AudioClip, director } from 'cc';
const { ccclass, property } = _decorator;

import { SSS_Consts } from "../../../../Script/Data/SSS_Consts";
import { bdlMgr } from "../../../../Script/Manager/BundleManager_S";
import SSS_GameEventManager, { SS_GameEventData } from "../../../../Script/Manager/SSS_GameEventManager";
import { DragTetris_GameData } from "../Data/DragTetris_GameData";
import { DragTetris_GameStorage } from "../Data/DragTetris_GameStorage";
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';
import AdManager from 'db://assets/Script/Manager/AdManager';
import { SSS_Lockable } from 'db://assets/Script/Common/SSS_Lockable';
import SSS_ShuaiFunction from 'db://assets/Script/Utils/SSS_ShuaiFunction';

@ccclass('DragTetrisLosePanel')
export default class DragTetris_LosePanel extends Component {
    public static Instance: DragTetris_LosePanel;
    @property(Node)
    public Button_menu: Node | null = null;
    @property(Node)
    public Button_replay: Node | null = null;
    @property(Node)
    public Button_revive: Node | null = null;
    @property(Label)
    Label_score: Label | null = null;
    @property(Label)
    Label_high_score: Label | null = null;
    index: number;
    onLoad() {
        DragTetris_LosePanel.Instance = this;
    }
    ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh();

        let audio_root = `audio/fail`;
        bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
            SSS_AudioManager.Instance.playSFX(res, false);
        });
    }
    ShowOff() {
        this.node.destroy();
        this.RemoveListener();
    }
    Refresh() {
        this.Label_score.string = `${DragTetris_GameData.GetScore()}`;
        this.Label_high_score.string = `${DragTetris_GameStorage.data.high_score}`;
    }
    AddListener() {
        this.Button_menu.on(Node.EventType.TOUCH_END, this.On_Button_menu, this);
        this.Button_replay.on(Node.EventType.TOUCH_END, this.On_Button_replay, this);
        this.Button_revive.on(Node.EventType.TOUCH_END, this.On_Button_revive, this);
    }
    RemoveListener() {
        this.Button_menu.off(Node.EventType.TOUCH_END, this.On_Button_menu, this);
        this.Button_replay.off(Node.EventType.TOUCH_END, this.On_Button_replay, this);
        this.Button_revive.off(Node.EventType.TOUCH_END, this.On_Button_revive, this);
    }
    On_Button_menu() {
        // console.log(`On_Button_menu`);

        this.ShowOff();
        SSS_GameEventManager.eventTarget.emit(SS_GameEventData.Menu);
        SSS_AudioManager.PlayButtonAudio();
    }
    On_Button_replay() {
        // console.log(`On_Button_replay`);

        this.ShowOff();
        SSS_GameEventManager.eventTarget.emit(SS_GameEventData.Replay);
        SSS_AudioManager.PlayButtonAudio();
    }

    @SSS_Lockable(1000)
    On_Button_revive() {
        SSS_ShuaiFunction.AD_callback(() => {
            console.log("复活成功！");

            let audio_root = `audio/life received`;
            bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
                SSS_AudioManager.Instance.playSFX(res, false);
            });

            this.ShowOff();
            SSS_GameEventManager.eventTarget.emit(SS_GameEventData.Revive);
            SSS_AudioManager.PlayButtonAudio();
        });


        // AdManager.是否看完广告 = false;
        // AdManager.Instance.showRewardVideoAd(AdManager.AdsIds.id_reward_vertical, {},
        //     () => {
        //         director.emit(SS_GameEventData.广告结束);
        //         console.log("复活成功！");



        //         let audio_root = `audio/life received`;
        //         bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
        //             SSS_AudioManager.Instance.playSFX(res, false);
        //         });

        //         this.ShowOff();
        //         SSS_GameEventManager.eventTarget.emit(SS_GameEventData.Revive);
        //         SSS_AudioManager.PlayButtonAudio();

        //     },
        //     () => {
        //         director.emit(SS_GameEventData.广告结束);

        //         console.log("复活失败！");
        //     },
        // );
    }
}